package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.TensionRecording;
import com.ruoyi.zhgd.domain.TensionResult;
import com.ruoyi.zhgd.mapper.TensionRecordingMapper;
import com.ruoyi.zhgd.mapper.TensionResultMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjTensionReportEditBo;
import com.ruoyi.zhgd.domain.vo.ZjTensionReportEditVo;
import com.ruoyi.zhgd.domain.ZjTensionReportEdit;
import com.ruoyi.zhgd.mapper.ZjTensionReportEditMapper;
import com.ruoyi.zhgd.service.IZjTensionReportEditService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 张拉报编辑信息保存Service业务层处理
 *
 * @author tll
 * @date 2023-12-08
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjTensionReportEditServiceImpl implements IZjTensionReportEditService {

    private final ZjTensionReportEditMapper baseMapper;

    private final TensionRecordingMapper tensionRecordingMapper;

    private final TensionResultMapper tensionResultMapper;

    /**
     * 查询张拉报编辑信息保存
     */
    @Override
    public ZjTensionReportEditVo queryById(String id){
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("主键ID不能为空");
        }
        // 根据id查询recording表的设计砼强度和实际砼强度
        TensionRecording recording = tensionRecordingMapper.getById(id);
        QueryWrapper<ZjTensionReportEdit> wrapper = new QueryWrapper<>();
        wrapper.eq("business_id", id);
        ZjTensionReportEditVo vo = baseMapper.selectVoOne(wrapper);
        if (ObjectUtils.isNotEmpty(vo)) {
            vo.setConcreteDesign(recording.getConcreteDesign());
            vo.setConcreteStrength(recording.getConcreteStrength());
            return vo;
        } else {
            ZjTensionReportEditVo data = new ZjTensionReportEditVo();
            data.setConcreteDesign(recording.getConcreteDesign());
            data.setConcreteStrength(recording.getConcreteStrength());
            return data;
        }
    }

    /**
     * 查询张拉报编辑信息保存列表
     */
    @Override
    public TableDataInfo<ZjTensionReportEditVo> queryPageList(ZjTensionReportEditBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjTensionReportEdit> lqw = buildQueryWrapper(bo);
        Page<ZjTensionReportEditVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询张拉报编辑信息保存列表
     */
    @Override
    public List<ZjTensionReportEditVo> queryList(ZjTensionReportEditBo bo) {
        LambdaQueryWrapper<ZjTensionReportEdit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjTensionReportEdit> buildQueryWrapper(ZjTensionReportEditBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjTensionReportEdit> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getReportId()), ZjTensionReportEdit::getReportId, bo.getReportId());
        lqw.eq(bo.getCreateDate() != null, ZjTensionReportEdit::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateUser()), ZjTensionReportEdit::getCreateUser, bo.getCreateUser());
        lqw.eq(bo.getUpdateDate() != null, ZjTensionReportEdit::getUpdateDate, bo.getUpdateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateUser()), ZjTensionReportEdit::getUpdateUser, bo.getUpdateUser());
        lqw.eq(StringUtils.isNotBlank(bo.getGjlq()), ZjTensionReportEdit::getGjlq, bo.getGjlq());
        lqw.eq(StringUtils.isNotBlank(bo.getGcgg()), ZjTensionReportEdit::getGcgg, bo.getGcgg());
        lqw.eq(StringUtils.isNotBlank(bo.getTemp()), ZjTensionReportEdit::getTemp, bo.getTemp());
        lqw.eq(StringUtils.isNotBlank(bo.getDsbfl()), ZjTensionReportEdit::getDsbfl, bo.getDsbfl());
        lqw.eq(StringUtils.isNotBlank(bo.getYb()), ZjTensionReportEdit::getYb, bo.getYb());
        lqw.eq(StringUtils.isNotBlank(bo.getQjd()), ZjTensionReportEdit::getQjd, bo.getQjd());
        return lqw;
    }

    /**
     * 新增张拉报编辑信息保存
     */
    @Override
    public Boolean insertByBo(ZjTensionReportEditBo bo) {
        ZjTensionReportEdit add = BeanUtil.toBean(bo, ZjTensionReportEdit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改张拉报编辑信息保存
     */
    @Override
    @Transactional
    public Boolean updateByBo(ZjTensionReportEditBo bo) {
        if(ObjectUtils.isEmpty(bo.getId())) {
            throw new ServiceException("主键ID不能为空");
        }
        // 根据id查询主表id
        TensionRecording recording = tensionRecordingMapper.getById(bo.getId());
        // 更新主表 设计砼强度 实际砼强度
//        if (ObjectUtils.isNotEmpty(bo.getConcreteDesign())) {
//            recording.setConcreteDesign(bo.getConcreteDesign());
//            tensionRecordingMapper.updateById(recording);
//        }
//        if (ObjectUtils.isNotEmpty(bo.getConcreteStrength())) {
//            recording.setConcreteStrength(bo.getConcreteStrength());
//            tensionRecordingMapper.updateById(recording);
//        }
        LambdaQueryWrapper<TensionRecording> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getId()), TensionRecording::getId, bo.getId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getConcreteDesign()), TensionRecording::getConcreteDesign, bo.getConcreteDesign());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getConcreteStrength()), TensionRecording::getConcreteStrength, bo.getConcreteStrength());
        lqw.eq(StringUtils.isNotBlank(bo.getJackCode1()), TensionRecording::getJackCode1, bo.getJackCode1());
        lqw.eq(StringUtils.isNotBlank(bo.getJackCode2()), TensionRecording::getJackCode2, bo.getJackCode2());
        lqw.eq(StringUtils.isNotBlank(bo.getSensorCode1()), TensionRecording::getSensorCode1, bo.getSensorCode1());
        lqw.eq(StringUtils.isNotBlank(bo.getSensorCode2()), TensionRecording::getSensorCode2, bo.getSensorCode2());
        lqw.eq(StringUtils.isNotBlank(bo.getZjComments()), TensionRecording::getZjComments, bo.getZjComments());
        lqw.eq(StringUtils.isNotBlank(bo.getJlComments()), TensionRecording::getJlComments, bo.getJlComments());
        lqw.eq(StringUtils.isNotBlank(bo.getZjRq()), TensionRecording::getZjRq, bo.getZjRq());
        lqw.eq(StringUtils.isNotBlank(bo.getJlRq()), TensionRecording::getJlRq, bo.getJlRq());
        tensionRecordingMapper.update(recording, lqw);

        // 保存张拉编辑表
        QueryWrapper<ZjTensionReportEdit> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("business_id", bo.getId());
        ZjTensionReportEdit edit = baseMapper.selectOne(wrapper1);
        if (ObjectUtils.isNotEmpty(edit)) {
            edit.setGjlq(bo.getGjlq());
            edit.setGcgg(bo.getGcgg());
            edit.setTemp(bo.getTemp());
            edit.setDsbfl(bo.getDsbfl());
            edit.setYb(bo.getYb());
            edit.setQjd(bo.getQjd());
            edit.setBridgeName(bo.getBridgeName());
            edit.setUpdateUser(LoginHelper.getUserId().toString());
            edit.setUpdateDate(new Date());
            baseMapper.updateById(edit);
        } else {
            edit = new ZjTensionReportEdit();
            edit.setGjlq(bo.getGjlq());
            edit.setGcgg(bo.getGcgg());
            edit.setTemp(bo.getTemp());
            edit.setDsbfl(bo.getDsbfl());
            edit.setYb(bo.getYb());
            edit.setQjd(bo.getQjd());
            edit.setBridgeName(bo.getBridgeName());
            edit.setCreateUser(LoginHelper.getUserId().toString());
            edit.setCreateDate(new Date());
            edit.setBusinessId(bo.getId());
            edit.setDelFlag("0");
            baseMapper.insert(edit);
        }
        // 更新result表的每束钢材长度,滑断丝数
        if (ObjectUtils.isNotEmpty(bo.getList())) {
            for (TensionResult result : bo.getList()) {
                // 根据孔号和linkCode查询对应的result数据
                QueryWrapper wrapper2 = new QueryWrapper();
                wrapper2.eq("link_code", result.getLinkCode());
                wrapper2.eq("hole_number", result.getHoleNumber());
                List<TensionResult> list = tensionResultMapper.selectList(wrapper2);
                for (TensionResult tensionResult : list) {
                    tensionResult.setHoleLen(result.getHoleLen());
                    tensionResult.setHdsNumber(result.getHdsNumber());
                }
                tensionResultMapper.updateBatchById(list);
            }
        }
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjTensionReportEdit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除张拉报编辑信息保存
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
